//
//  main.m
//  HomeWork_OC_06
//
//  Created by lanou on 15/9/26.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    
    /*
     * 作业一:  创建1个包含5个字符串元素的数组, 完成以下两点需求
     */
    
    
    //1.1: 用for循环倒序输出所有元素
    NSArray * array = [NSArray arrayWithObjects:@"asd", @"dfdfd", @"12", @"af", nil];
    
    for (int i = 0; i < array.count; i++) {
        NSLog(@"%@", [array objectAtIndex:i]);
    }
    
    
    //1.2: 用for...in... 完成所有字符串元素的拼接(生成一个 新的字符串), 并输出
    NSMutableString * appendStrings = [NSMutableString string];
    
    for (NSString * str in array) {
        [appendStrings appendString:str];
    }
    NSLog(@"%@", appendStrings);
    
    
    
    
    /*
     * 作业2: 创建1个包含5个person对象的数组(姓名用英文), 完成以下两点需求
     */
    Person * per1 = [[Person alloc] initWithName:@"zhang_m_30" age:30 sex:@"m"];
    Person * per2 = [[Person alloc] initWithName:@"wang _f_26" age:26 sex:@"f"];
    Person * per3 = [[Person alloc] initWithName:@"cao  _m_11" age:11 sex:@"m"];
    Person * per4 = [[Person alloc] initWithName:@"li   _f_18" age:17 sex:@"f"];
    Person * per5 = [[Person alloc] initWithName:@"deng _m_42" age:42 sex:@"m"];
    
    NSMutableArray * personAry = [NSMutableArray arrayWithObjects:per1, per2, per3, per4, per5, nil];
    
    
    
    //2.1: 用for循环找到年龄最大的那个
    Person * maxAgePerson = personAry[0];
    
    for (int i = 0; i < personAry.count; i++) {
        
        
        
        if (maxAgePerson.age < [[personAry objectAtIndex:i] age]) {
            
            maxAgePerson = [personAry objectAtIndex:i];
        }
    }
    
    NSLog(@"maxAge: %d, name = %@", maxAgePerson.age, maxAgePerson.name);
    
    
    //2.2: 用for...in遍历数组,把大于某个年龄的姓名后面拼接上(-brother)
    for (Person * person in personAry) {
        
        if (person.age >= 30) {
            
            person.name = [person.name stringByAppendingString:@"-brother"];
        }
    }
    
    
    
    /*
     * 作业3: 使用第二题目中生成的数组, 完成以下需求两点需求
     */
    
    //3.1: 使用sortUsingSelector: 分别按年龄, 姓名, 性别进行排序, 输出排序结果
    
    [personAry sortUsingSelector:@selector(sortByAge:)];
    NSLog(@"%@", personAry);
    
    
    
    [personAry sortUsingSelector:@selector(sortByName:)];
    NSLog(@"%@", personAry);
    
    
    [personAry sortUsingSelector:@selector(sortBySex:)];
    NSLog(@"%@", personAry);
    
    
    
    
    //3.2: 使用NSSortDescriptor, 按性名进行排序
    NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    NSArray * descriptorSortArray = [personAry sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
    
    NSLog(@"%@", descriptorSortArray);
    
    
    
    
    
    return 0;
}
